﻿using FolderTag.DB.Dbo;
using FolderTag.Model;
using System.Collections.Generic;
using System.Linq;

namespace FolderTag.DB
{
    /// <summary>
    /// 快捷访问Db类
    /// </summary>
    public static class QuickPathDb
    {
        private static object _LockObject = new object();

        private static List<QuickPathItem> _QuickList = null;
        /// <summary>
        /// 添加快捷访问
        /// </summary>
        /// <param name="tagName"></param>
        public static bool Add(QuickPathItem tagItem)
        {
            return DBHelper.SQL_R.Insert(QuickPathDbo.CreateNew(tagItem), false) > 0;
        }

        /// <summary>
        /// 删除快捷访问
        /// </summary>
        /// <param name="id"></param>
        public static bool Delete(int id)
        {
            return DBHelper.SQL_R.Delete<QuickPathDbo>(id) > 0;
        }

        /// <summary>
        /// 查询列表
        /// </summary>
        /// <returns></returns>
        public static List<QuickPathItem> QueryList(bool isRefresh = false)
        {
            if (_QuickList == null || isRefresh)
            {
                lock (_LockObject)
                {
                    if (_QuickList == null || isRefresh)
                        _QuickList = DBHelper.SQL_R.FindList<QuickPathDbo>().Select(x => new QuickPathItem(x)).ToList();

                }
            }
            return _QuickList ?? new List<QuickPathItem>();
        }
    }
}
